//
//  AddTargetViewController.swift
//  Accounting
//
//  Created by Qingke on 16/5/9.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit
//protocol AddTargetDelegate {
//    func addtargetDelegateViewController(viewcontroller: AddTargetViewController,customDate: String,custonIcon:UIImage,mark: String)
//}
class AddTargetViewController: UITableViewController {

    @IBOutlet weak var inputSymbol: UILabel!
    @IBOutlet weak var outputSymbol: UILabel!
    @IBOutlet weak var textFiled: UITextField!
    @IBOutlet weak var CompleteButton: UIBarButtonItem!

    @IBOutlet weak var iconImage: UIImageView!
    @IBOutlet weak var mark: UITextField!

    var iconName: String?
    var dataModel: DataModel!

    
    var iconCategory: IconCategory?

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let controller = UIApplication.sharedApplication().delegate as! AppDelegate
        dataModel = controller.dataModel
        
        iconCategory = IconCategory()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    
    // MARK: - Table view data source

    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 3
    }
    func nowTime() -> String {
        let date = NSDate()
        let timeFormatter = NSDateFormatter()
        timeFormatter.dateFormat = "   'at' HH:mm" //(格式可俺按自己需求修整)
        let strNowTime = timeFormatter.stringFromDate(date) as String
        return strNowTime
    }
    @IBAction func inputTouch(sender: UIGestureRecognizer) {
        textFiled.resignFirstResponder()
        if inputSymbol.text == "√" {
            inputSymbol.text = ""
            outputSymbol.text = "√"
        } else {
            inputSymbol.text = "√"
            outputSymbol.text = ""
        }
    }
    @IBAction func CompleteButton(sender: UIBarButtonItem) {
        if let addMoney:Double = Double(textFiled.text!){
//            CompleteButton.enabled = true
        if inputSymbol.text == "√" {
            NSUserDefaults.standardUserDefaults().setDouble((addMoney + NSUserDefaults.standardUserDefaults().doubleForKey("inputMoney")), forKey: "inputMoney")
        } else  if outputSymbol.text == "√" {
            NSUserDefaults.standardUserDefaults().setDouble(addMoney + NSUserDefaults.standardUserDefaults().doubleForKey("outputMoney"), forKey: "outputMoney")
            }
        //将本页的数据全部保存到本地文件中，在TargetDetail中显示出来
//            dataModel.iconCategory.customDate = date
//            dataModel.iconCategory.customMark = mark.text ?? ""
//            dataModel.iconCategory.customAddMoney = addMoney
//            dataModel.saveData()
//            
            iconCategory?.customDate = nowTime()
            iconCategory?.customMark = mark.text ?? ""
            iconCategory?.customAddMoney = addMoney
            iconCategory?.customIcon = iconImage.image
            
            dataModel.iconCategory.append(iconCategory!)
            
            dismissViewControllerAnimated(true, completion: nil)
        }
    }
    @IBAction func cancelButton(sender: UIBarButtonItem) {
        dismissViewControllerAnimated(true, completion: nil)
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let controller = segue.destinationViewController as! TargetIconPicker
        controller.iconDelegate = self
    }

}
extension AddTargetViewController: UITextFieldDelegate {
    func textFieldDidEndEditing(textField: UITextField) {
        if (textFiled.text != nil) {
            CompleteButton.enabled = true
        }
    }
}
extension AddTargetViewController: IconPickDelegate {
    func iconpickDelegateViewController(viewcontroller: TargetIconPicker, icon: String) {
        self.iconName = icon
        iconImage.image = UIImage(named: iconName!)
    }
    
}
